Public Class frmLogin
    Dim MiGestDatos As New GestDatos()
    Dim MiDataView As Data.DataView
    Dim MiCursorRaton As New GestProyecto
    
    Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click

        MiCursorRaton.CursorRaton(True)
        If txtUsuario.Text = "" Then
            MessageBox.Show("Introduzca un usuario")
            Habilitar()
            txtUsuario.Focus()
            MiCursorRaton.CursorRaton(False)
            Exit Sub
        End If

        If txtClave.Text = "" Then
            MessageBox.Show("Introduzca una Clave")
            Habilitar()
            txtClave.Focus()
            MiCursorRaton.CursorRaton(False)
            Exit Sub
        End If

        btnAceptar.Enabled = False
        btnCancelar.Enabled = False
        txtUsuario.Enabled = False
        txtClave.Enabled = False

        ConectarDB()

        If ComprobarUsuario() = False Then
            MsgBox("Usuario incorrecto")
            Habilitar()
            MiCursorRaton.CursorRaton(False)
            txtUsuario.Focus()
        Else
            If ComprobarClave(txtClave.Text) = False Then
                MsgBox("Clave incorrecta")
                Habilitar()
                MiCursorRaton.CursorRaton(False)
                txtClave.Focus()
            Else
                frmInicio.btnInicio.Enabled = False
                frmInicio.btnAlmacen.Enabled = True
                frmInicio.btnPedidos.Enabled = True
                frmInicio.btnClientes.Enabled = True
                MiGestDatos.CerrarDataSet()
                frmInicio.lblagente.Text = txtUsuario.Text
                MiCursorRaton.CursorRaton(False)
                Me.Dispose()
            End If
        End If

    End Sub

    Private Sub ConectarDB()


        Dim RutaConexion As String
        RutaConexion = "Data Source =" & System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) & "\DBGestion.sdf;"

        Dim LineaSQL(0) As String
        Dim Tablas(0) As String

        LineaSQL(0) = "Select CodUsuario, Clave From Usuarios"
        Tablas(0) = "Usuarios"

        MiGestDatos.ConextarBD(RutaConexion, LineaSQL, Tablas)

    End Sub

    Private Sub btnBUsuario_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBUsuario.Click

        MiCursorRaton.CursorRaton(True)

        frmInicio.MifrmBUsuario = New frmBUsuario
        frmInicio.MifrmBUsuario.Show()

        MiCursorRaton.CursorRaton(False)

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Dispose()
    End Sub

    Private Sub Habilitar()
        btnAceptar.Enabled = True
        btnCancelar.Enabled = True
        txtUsuario.Enabled = True
        txtClave.Enabled = True
    End Sub

    Private Function ComprobarClave(ByVal Clave As String) As Boolean

        If Clave = MiDataView.ToTable.Rows(0)("Clave") Then
            Return True
        Else
            Return False
        End If

    End Function

    Private Function ComprobarUsuario() As Boolean
        Dim Filtro As String = "CodUsuario = '" & txtUsuario.Text & "'"
        Dim DataTable As Data.DataTable = MiGestDatos.DataSet.Tables("Usuarios")
        midataview = DataTable.DefaultView

        MiDataView.RowFilter = Filtro
        If MiDataView.Count = 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    Private Sub frmLogin_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        MiCursorRaton.CursorRaton(True)

        ConectarDB()

        MiCursorRaton.CursorRaton(False)

    End Sub
End Class

